package com.person.sys.action;



import java.util.List;
import java.util.regex.Pattern;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ActionContext;
import com.person.commons.struts2.BaseAction;
import com.person.sys.model.SysCode;
import com.person.sys.service.SysCodeService;

public class SysCodeAction extends BaseAction {
      private SysCode scode;
      private SysCodeService sysCodeService;
      private List<SysCode> codelist;
      private String codeId;
  	private String codeNo;
  	private String codeName;
  	private String codeOrder;
  	private String codeNoParent;
  	private String codeFlag;
  	private String nextAction;
  	private String selectNo;//用于保存在增加二级代码时，默认选择的主分类代码编号
      
      

  	/**
  	 * 返回代码管理框架页
  	 * @return
  	 */
  	  	@SkipValidation
  	      public String index(){
  	    		 return "codeall";
  	      }
      
  		/**
  	  	 * 返回代码管理框架页
  	  	 * @return
  	  	 */
  	  	  	@SkipValidation
  	  	      public String allindex(){
  	  	    		 return "index";
  	  	      }
  	  	
      /**
       * 返回系统代码管理框架右侧默认页
       * 
       */
  	@SkipValidation
      public String blank(){
    		 return "blank";
      }
      
      
      /**
       * 返回添加系统顶级代码页面
       * 
       */
  	@SkipValidation
      public String createTop(){
    		 return "input";
      }
      
      /**
       * 查找系统主分类代码
       * 用于返回到左边的管理页面
       */
  	@SkipValidation
      public String admin(){
    	  codelist=sysCodeService.searchAllTopCode();
    	 if(codelist.size()>0){
    		 return "admin";
    	 }else{
       	  return "info"; 
    	 }

      }
        
      /**
       * 查找系统主分类代码的编号与名称
       * 用于添加下级代码使用
       */
  	@SkipValidation
      public String searchTopCodeNo(){
    	  setSelectNo(codeId);
//    	  //获得代码主分类的代码名称
    	 codelist=sysCodeService.searchAllTopCode();
    	 if(codelist.size()>0){
    		 return "createNextLevelCode";
    	 }else{
       	  return "info"; 
    	 }
      }
  	
  	
  	  
      
      
      /**
       * 通过系统编号查找系统分类代码
       * 用于编辑代码
       */
  	  @SkipValidation
      public String searchCodeByNo(){

    	 if(sysCodeService.searchCodeByNo(codeNo, codeNoParent)!=null){
        	 scode=sysCodeService.searchCodeByNo(codeNo, codeNoParent).get(0);
    		 return "modifyTopCode";
    	 }else{
       	  return "info"; 
    	 }
      }
      
  	/**
       * 通过系统编号查找系统分类代码
       * 用于编辑代码
       */
  	  @SkipValidation
      public String searchCodeById(){
        	 scode=sysCodeService.searchCodeById(codeId);
    		 return "modifyTopCode";
      }
        
      /**
       * 通过传入的上级系统代码编号查找该代码下的所有下级代码
  	 * @author wang zitao
  	 * @prama codeno本级代码编号，在查询时与父代码相对应
  	 * @return 代码信息列表
  	 */
  	  @SkipValidation
      public String searchNextCodeByNo(){
    	  //获取主分类代码编号和名字
    	  SysCode topcode=sysCodeService.searchCodeById(codeId);
    	  //获得二级分类代码列表
    	  codelist=sysCodeService.searchNextCodeByNo(topcode.getCodeNo());
    	  setCodeNo(topcode.getCodeNo());
    	  setCodeName(topcode.getCodeName());
    	  setCodeId(topcode.getCodeId());
    		 return "showNextCode";
      }
  	  
  	  
  	/**
       * 通过传入的二级系统代码编号和父代码编号查找父代码下的所有二级代码
       * 该action用于在二级代码添加完成后，跳向相应的二级代码管理页面
  	 * @author wang zitao
  	 * @prama codeno二级代码编号
  	 * @return 代码信息列表
  	 */
  	  @SkipValidation
      public String searchNext(){
    	  //获得二级分类代码列表
    	  codelist=sysCodeService.searchNextCodeByNo(codeNoParent);
    	  //获取主分类代码编号和名字
    	  SysCode topcode=sysCodeService.searchCodeByNo(codeNoParent, null).get(0);
    	  setCodeNo(topcode.getCodeNo());
    	  setCodeName(topcode.getCodeName());
    	  setCodeId(topcode.getCodeId());
    		 return "showNextCode";
      }
      
      
      /**
       * 通过系统编号查找系统二级分类代码信息
       * 用于编辑代码
       */
  	  @SkipValidation
      public String searchNextCode(){
          //二级代码的详细信息
    	  scode=sysCodeService.searchCodeById(codeId);
    	  setCodeNo(scode.getCodeNo());
    	  setCodeName(scode.getCodeName());
    	  //代码列表
    	  codelist=sysCodeService.searchCodeByNo(scode.getCodeNoParent(),null);
    	 if(scode!=null){
    		 return "modifyNextLevelCode";
    	 }else{
       	  return "info"; 
    	 }
      }
      
     /**
      * 系统主分类代码添加
      */ 
  	  
      public String createTopCode(){
    	  if(scode.getCodeNo()==null||"".equals(scode.getCodeNo())){
   		   info="编号不可为空";
   	     return "fail";}
   	   if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
   		   info="名称不可为空";
	    	  return "fail";}
    	 char s=sysCodeService.createCode(scode);
    	 if(s=='1'){
    	  info=getText("create.Success");
    		 return "blank";
    	  }
    	 else if(s=='2')
    	 {
    		 info=getText("modify.Repeat");
    		 return "info";
    	 }
    	 else
    	 {
    		 info=getText("create.Fail");
    		 return "info";
    	 }
      }
      
      /**
       * 系统二级分类代码添加
       */ 
       public String createNextLevelCode(){
    	   //数据检验
    	   if(scode.getCodeNo()==null||"".equals(scode.getCodeNo())){
    		   info="编号不可为空";
    	     return "fail";}
    	   if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
    		   info="名称不可为空";
 	    	  return "fail";}
    	   //保存新纪录
    	 scode.setCodeNoParent(codeNoParent);
     	 char s=sysCodeService.createNextCode(scode);
     	 if(s=='1'){
     		 setCodeNo(scode.getCodeNo());
     		 setCodeNoParent(scode.getCodeNoParent());
     		 return "showNextCode";
     	  }
     	 else if(s=='2')
     	 {
     		info="代码重复";
     		 return "fail";
     	 }
     	 else
     	 {
     		info="创建失败";
     		 return "info";
     	 }
     	 
       }
      /**
       * 系统代码编辑
       */
      public String modifyCode(){
    	  char s=sysCodeService.modifyCode(scode);
    	  if(s=='1'){
    		  info="修改成功";
        		 return "blank";
        	  }
        	 else if(s=='2')
        	 {
        		 info="代码重复";
        		 return "input";
        	 }
        	 else
        	 {
        		 info="修改失败";
        		 return "fail";
        	 }
      }

      
      
      /**
       * 系统二级代码编辑
       */
      public String modifyNextLevelCode(){
    	  //数据检验
    	  scode.setCodeNoParent(codeNoParent);
    	  if(scode.getCodeNo()==null||"".equals(scode.getCodeNo())){
    		  info="编号不可为空";
	     return "fail";}
	   if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
		   info="名称不可为空";
	    	  return "fail";}
          //修改代码
    	  char s=sysCodeService.modifyCode(scode);

    	  if(s=='1'){
        	  setCodeNo(scode.getCodeNoParent());//因为是二级代码，所以要将其父代码作为查询的代码传入，才可得到二级代码列表
        	  setCodeNoParent(null);
            	return "showNextCode";
        	  }
        	 else if(s=='2')
        	 {
        		 info="代码重复";
        		 return "input";
        	 }
        	 else
        	 {
        		 info="修改失败";
        		 return "fail";
        	 }
      }

      
      /**
       * 删除顶级代码
       */
      @SkipValidation
      public String deleteCode(){
    	  char s=sysCodeService.deleteCode(codeId,codeNo);
    	  if('1'==s){
    	  return "admin";
    	  }
    	  else{
    	  return "info";
    	  }
      }
      
      
      /**
       * 删除二级系统代码
       */
      @SkipValidation
      public String deleteNextCode(){
    	  char s=sysCodeService.deleteNextCode(codeId);
    	  if('1'==s){
    	  return "showNextCode";
    	  }
    	  else{
    	  return "info";
    	  }
      }

      
      public void validateCreateTopCode(){
  		if(scode.getCodeNo()==null ||"".equals(scode.getCodeNo())){
  			addFieldError("scode.codeNo",getText("error.codeNo"));
  		}
  		else if(!Pattern.matches("^\\w+$", scode.getCodeNo().trim()))
		{
  			addFieldError("scode.codeNo",getText("error.codeNostr"));
		}
  		if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
  			addFieldError("scode.codeName",getText("error.codeName"));
  		}
  		if(scode.getCodeOrder()==null ||"".equals(scode.getCodeOrder())){
  			addFieldError("scode.codeOrder",getText("error.codeOrder"));
  		}
  	}
      
      
      public void validateModifyCode(){
    		if(scode.getCodeNo()==null ||"".equals(scode.getCodeNo())){
    			addFieldError("scode.codeNo",getText("error.codeNo"));
    		}
    		else if(!Pattern.matches("^\\w+$", scode.getCodeNo().trim()))
    		{
      			addFieldError("scode.codeNo",getText("error.codeNostr"));
    		}
    		if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
    			addFieldError("scode.codeName",getText("error.codeName"));
    		}
    		if(scode.getCodeOrder()==null ||"".equals(scode.getCodeOrder())){
    			addFieldError("scode.codeOrder",getText("error.codeOrder"));
    		}
    	}
      
      
      public void validateCreateNextLevelCode(){
  		if(scode.getCodeNo()==null ||"".equals(scode.getCodeNo())){
  			addFieldError("scode.codeNo",getText("error.codeNo"));
  		}
  		else if(!Pattern.matches("^\\w+$", scode.getCodeNo().trim()))
		{
  			addFieldError("scode.codeNo",getText("error.codeNostr"));
		}
  		if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
  			addFieldError("scode.codeName",getText("error.codeName"));
  		}
  		if(scode.getCodeOrder()==null ||"".equals(scode.getCodeOrder())){
  			addFieldError("scode.codeOrder",getText("error.codeOrder"));
  		}
  	}
      
      
      public void validateModifyNextLevelCode(){
    		if(scode.getCodeNo()==null ||"".equals(scode.getCodeNo())){
    			addFieldError("scode.codeNo",getText("error.codeNo"));
    		}
    		else if(!Pattern.matches("^\\w+$", scode.getCodeNo().trim()))
    		{
      			addFieldError("scode.codeNo",getText("error.codeNostr"));
    		}
    		if(scode.getCodeName()==null||"".equals(scode.getCodeName())){
    			addFieldError("scode.codeName",getText("error.codeName"));
    		}
    		if(scode.getCodeOrder()==null ||"".equals(scode.getCodeOrder())){
    			addFieldError("scode.codeOrder",getText("error.codeOrder"));
    		}
    	}
      
	public SysCodeService getSysCodeService() {
		return sysCodeService;
	}

	public void setSysCodeService(SysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}

	public List<SysCode> getCodelist() {
		return codelist;
	}

	public void setCodelist(List<SysCode> codelist) {
		this.codelist = codelist;
	}

	public String getCodeNo() {
		return codeNo;
	}

	public void setCodeNo(String codeNo) {
		this.codeNo = codeNo;
	}

	public String getCodeName() {
		return codeName;
	}

	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}

	public String getCodeNoParent() {
		return codeNoParent;
	}

	public void setCodeNoParent(String codeNoParent) {
		this.codeNoParent = codeNoParent;
	}

	public String getCodeId() {
		return codeId;
	}

	public void setCodeId(String codeId) {
		this.codeId = codeId;
	}

	public String getCodeOrder() {
		return codeOrder;
	}

	public void setCodeOrder(String codeOrder) {
		this.codeOrder = codeOrder;
	}

	public String getCodeFlag() {
		return codeFlag;
	}

	public void setCodeFlag(String codeFlag) {
		this.codeFlag = codeFlag;
	}


	public SysCode getScode() {
		return scode;
	}


	public void setScode(SysCode scode) {
		this.scode = scode;
	}


	public String getNextAction() {
		return nextAction;
	}


	public void setNextAction(String nextAction) {
		this.nextAction = nextAction;
	}


	public String getSelectNo() {
		return selectNo;
	}


	public void setSelectNo(String selectNo) {
		this.selectNo = selectNo;
	}
}
